package com.auto.admin.manager.impl;

import com.auto.admin.constants.Constants;
import com.auto.admin.entity.Beans;
import com.auto.admin.entity.BeansDependencies;
import com.auto.admin.entity.bo.param.BeansBO;
import com.auto.admin.manager.BeansManager;
import com.auto.admin.service.BeansDependenciesService;
import com.auto.admin.service.BeansService;
import com.auto.api.common.utils.BeanMapper;
import com.auto.api.common.utils.IdGenerator;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * Description:业务处理类
 *
 * @author liangshf
 * @date 2019-06-20
 */
@Service
public class BeansManagerImpl implements BeansManager {

    @Resource
    private BeansService beansService;

    @Resource
    private BeansDependenciesService beansDependenciesService;

    @Async
    public void saveBeans(List<BeansBO> beansList) {
        beansList = checkBeansBO(beansList);
        //组装并解析api分类
        saveBeanList(beansList);
    }

    /**
     * 验证及组装beansBO信息
     *
     * @param beansList List<BeansBO>
     * @return List
     */
    private List<BeansBO> checkBeansBO(List<BeansBO> beansList) {
        //旧MockMappingBO
        Map<String, Beans> oldBeanMap = Collections.emptyMap();
        if (CollectionUtils.isNotEmpty(beansList)) {
            String applicationName = beansList.get(0).getApplicationName();
            List<Beans> oldList = beansService.findByApplicationName(applicationName);
            if (CollectionUtils.isNotEmpty(oldList)) {
                oldBeanMap = oldList.stream().collect(Collectors.toMap(Beans::getBeanName, Function.identity()));
            }
        }
        for (BeansBO beansBO : beansList) {
            Beans beans = oldBeanMap.remove(beansBO.getBeanName());
            if (beans != null) {
                beansBO.setId(beans.getId());
                List<BeansDependencies> beansDependenciesList = checkBeansDependenciesList(beansBO);
                beansBO.setBeansDependenciesList(beansDependenciesList);
            }
        }

        //标记已删除的项
        for (Beans beans : oldBeanMap.values()) {
            beans.setDel(Constants.ONE);
            beans.setUpdatedTime(LocalDateTime.now());
            BeansBO beansBO = BeanMapper.map(beans, BeansBO.class);
            List<BeansDependencies> beansDependencies = beansDependenciesService.findByBeanId(beans.getId());
            //标记已删除的项
            for (BeansDependencies dependencies : beansDependencies) {
                dependencies.setDel(Constants.ONE);
                dependencies.setUpdatedTime(LocalDateTime.now());
            }
            beansBO.setBeansDependenciesList(beansDependencies);
            beansList.add(beansBO);
        }
        return beansList;
    }

    /**
     *
     * 保存beansList信息
     * @param beansList    List<BeansBO>
     */
    private void saveBeanList(List<BeansBO> beansList) {
        if (beansList.size() > 1) {
            beansList.sort(Comparator.comparing(p -> p.getId() != null ? p.getId() : 0L));
        }
        for (BeansBO beansBO : beansList) {
            if (beansBO.getId() == null) {
                beansBO.setId(IdGenerator.nextId());
                if (beansBO.getBeanName().indexOf(Constants.POINT) != -1) {
                    beansBO.setSimpleBeanName(beansBO.getBeanName().substring(beansBO.getBeanName().lastIndexOf(Constants.POINT) + 1));
                } else {
                    beansBO.setSimpleBeanName(beansBO.getBeanName());
                }

                beansBO.setDel(Constants.ZERO);
                beansService.save(beansBO);
            }
            saveBeanDependList(beansBO);
        }
    }

    /**
     * 保存BeansBO信息
     *
     * @param beansBO BeansBO
     */
    private void saveBeanDependList(BeansBO beansBO) {
        List<BeansDependencies> beansDependencies = beansBO.getBeansDependenciesList();
        if (CollectionUtils.isEmpty(beansDependencies)) return;
        if (beansDependencies.size() > 1) {
            beansDependencies.sort(Comparator.comparing(p -> p.getId() != null ? p.getId() : 0L));
        }
        for (BeansDependencies beansDependent : beansDependencies) {
            beansDependent.setBeanId(beansBO.getId());
            if (beansDependent.getId() != null) {
                beansDependent.setUpdatedTime(LocalDateTime.now());
                beansDependenciesService.updateById(beansDependent);
            } else {
                beansDependent.setDel(Constants.ZERO);
                beansDependenciesService.save(beansDependent);
            }
        }
    }

    private List<BeansDependencies> checkBeansDependenciesList(BeansBO beansBO) {
        List<BeansDependencies> beansDependencies = beansDependenciesService.findByBeanId(beansBO.getId());
        Map<String, BeansDependencies> beansDependenciesMap = beansDependencies.stream().collect(Collectors.toMap(BeansDependencies::getDependencieName, Function.identity()));
        for (BeansDependencies beansDependen : beansBO.getBeansDependenciesList()) {
            beansDependenciesMap.remove(beansDependen.getDependencieName());
        }

        //标记已删除的项
        for (BeansDependencies dePenDen : beansDependenciesMap.values()) {
            dePenDen.setDel(Constants.ONE);
            dePenDen.setUpdatedTime(LocalDateTime.now());
            beansBO.getBeansDependenciesList().add(dePenDen);
        }
        return beansBO.getBeansDependenciesList();
    }
}
